package cn.itcast.controller;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * @Date :16:15 2021/6/13
 * @Author :xuhe
 */
@Controller
//@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView findUser(String userid,String password,HttpSession session){
        ModelAndView mav = new ModelAndView();
        User user=userService.findUser(userid,password);
        if (user==null){
            mav.addObject("message","用户名或密码错误");
            mav.setViewName("../../index");
        }
        else {
            System.out.println(userid+""+password);
            mav.addObject("user",user);
            session.setAttribute("user",user);
            mav.setViewName("forward:home_index");
        }

        return mav;
    }

    @RequestMapping("/register")
    public ModelAndView insertUser(MultipartFile avatar, String userid, String password, String username, String email, HttpServletRequest req){
        ModelAndView mav = new ModelAndView();
        User user=new User();
        //如果文件存在，isEmpty文件为空
        if(!(avatar.isEmpty())){
            //保存文件目录
            String path = req.getSession().getServletContext().getRealPath("/upload/");
            File newFile = new File(path);
            //判断文件目录存在不存在，如果不存在就创建目录
            if (!newFile.exists()){
                newFile.mkdirs();
            }
            String filename=avatar.getOriginalFilename();
            try {
                //转存文件
                avatar.transferTo(new File(path,filename));
                user.setImages("upload/"+filename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            //没有带图片,默认头像为微博
            user.setImages("images/icon.jpg");
        }
        user.setUserid(userid);
        user.setPassword(password);
        user.setUsername(username);
        user.setEmail(email);
        userService.saveUser(user);
        mav.addObject("user",user);
        mav.addObject("massage","注册成功");
        mav.setViewName("../../index");
        return mav;
    }

    @RequestMapping("/logintest")
    public String login(String userid, String password, HttpSession session, Model mav){

        User user=userService.findUser(userid,password);
        if (user==null){
            mav.addAttribute("message","用户名或密码错误");
           // mav.setViewName("../../index");
            return "../../index";
        }
        else {
            mav.addAttribute("user",user);
            session.setAttribute("user",user);
           // mav.setViewName("success");
            return "success";
        }
    }
    @RequestMapping("/update")
    public ModelAndView modifyUser(HttpSession session,MultipartFile avatar, String userid, String password, String username, String email, HttpServletRequest req){
        ModelAndView mav = new ModelAndView();
        //mav.setViewName("forward:modify");
        User user=(User)session.getAttribute("user");
        //如果文件存在，isEmpty文件为空
        if(!(avatar.isEmpty())){
            //保存文件目录
            String path = req.getSession().getServletContext().getRealPath("/upload/");
            File newFile = new File(path);
            //判断文件目录存在不存在，如果不存在就创建目录
            if (!newFile.exists()){
                newFile.mkdirs();
            }
            String filename=avatar.getOriginalFilename();
            try {
                //转存文件
                avatar.transferTo(new File(path,filename));
                user.setImages("upload/"+filename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            //没有带图片,默认头像为微博
            user.setImages("images/icon.jpg");
        }
        user.setUserid(user.getUserid());
        user.setPassword(password);
        user.setUsername(username);
        user.setEmail(email);
        userService.updateUserinfo(user);
        mav.addObject("user",user);
        mav.addObject("massage","修改成功");
        mav.setViewName("forward:home_index");
        return mav;
    }

}
